<?php

namespace V1\Model;


use Think\Exception;
use Think\Model;

class CategoryModel extends Model
{
    protected $tableName = 'category';

    /**
     * 获取栏目分类
     * @param null $cat_id
     * @param null $pid
     * @param null $cate_index
     * @param null $cate_name
     * @param null $status
     * @param null $userid [用户id]
     * @param string $type [分类类型]
     * @return mixed
     */
    public function get_category_list($cat_id = null, $pid = null, $cate_index = null, $cate_name = null, $status = null, $userid = null)
    {
        $map = [];
        if ($cat_id != null) {
            $map['id'] = $cat_id;
        }
        if ($pid != null) {
            $map['pid'] = $pid;
        }
        if ($cate_index != null) {
            $map['cate_index'] = $cate_index;
        }
        if ($cate_name != null) {
            $map['cat_name'] = $cate_name;
            $map['status'] = $status;
        }
        if ($status != null) {
            $map['status'] = $status;
        }
        $data = $this->where($map)->select();
        foreach ($data AS $k => $v) {
            $data[$k] = $v;
            $data[$k]['cat_img'] = 'Uploads/'.$v['cat_img'];
            //判断当前用户是否已经添加过该分类
            $data2 = D('SelectedCategory')->get_my_category($v['id'], $userid);
            if ($v['id'] == $data2[0]['cat_id']) {
                $data[$k]['is_ok'] = 1;
            } else {
                $data[$k]['is_ok'] = 0;
            }
            //圈子关注人数统计
            $data[$k]['member_count'] = D('SelectedCategory')->get_community_member_count($v['id']);
        }
        return $data;
    }

    /**
     * 获取栏目无限极分类
     * @param null $cat_id
     * @param int $pid
     * @param null $cate_index
     * @param null $cate_name
     * @param null $status
     * @param null $userid [用户id]
     * @return mixed
     */
    public function get_category_list_two($cat_id = null, $pid = 0, $cate_index = null, $cate_name = null, $status = null, $userid = null)
    {
        if ($pid == '') {
            $pid = 0;
        } else {
            $pid = $pid;
        }
        $map = [];
        if ($cat_id != null) {
            $map['id'] = $cat_id;
        }
        if ($cate_index != null) {
            $map['cate_index'] = $cate_index;
        }
        if ($cate_name != null) {
            $map['cat_name'] = $cate_name;
            $map['status'] = $status;
        }
        if ($status != null) {
            $map['status'] = $status;
        }
        $request = $this->where($map)->select();
        $data = $this->getOptionsAno($request, $pid, $userid);
        return $data;
    }

    private function getOptionsAno($list, $root = 0, $u_id)
    {
        $tree = array();
        foreach ($list as $key => $val) {

            if ($val['pid'] == $root) {
                //获取当前$pid所有子类
                unset($list[$key]);
                if (!empty($list)) {
                    $child = $this->getOptionsAno($list, $val['id'], $u_id);
                    if (!empty($child)) {
                        $val['child'] = $child;
                        $val['cat_img'] = 'Uploads/'.$val['cat_img'];
                    } else {
                        $val['child'] = [];
                    }
                }
                $data = D('SelectedCategory')->get_my_category($val['id'], $u_id);
                if ($val['id'] == $data[0]['cat_id']) {
                    $val['is_ok'] = 1;
                } else {
                    $val['is_ok'] = 0;
                }
                //圈子关注人数统计
                $val['member_count'] = D('SelectedCategory')->get_community_member_count($val['id']);
                $val['cat_img'] = 'Uploads/'.$val['cat_img'];
                $tree[] = $val;
            }
        }
        return $tree;
    }
}